<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.wef.system.entity.domain.OfficeDomain"%>
<%@include file="/pages/common/taglib.jsp"  %>
<html>
	<head>
		<title></title>
		<%@include file="/styles/css_lib.jsp"%>
		<%@include file="/scripts/widgets/ext-2.2.1/css_lib.jsp"%>
		<%@include file="/scripts/widgets/gfa4ext/css_lib.jsp"%>
		<%@include file="/scripts/widgets/autoSelect/css_lib.jsp"%>
		<%@include file="/pages/common/js_global.jsp"%>
		<%@include file="/scripts/sdk/js_lib.jsp"%>
		<%@include file="/scripts/config/js_lib.jsp"%>
		<%@include file="/scripts/utils/js_lib.jsp"%>
		<%@include file="/scripts/i18n/js_lib.jsp"%>
		<%@include file="/scripts/widgets/ext-2.2.1/js_lib.jsp"%>
		<%@include file="/scripts/widgets/gfa4ext/js_lib.jsp"%>
		<%@include file="/scripts/widgets/autoSelect/js_lib.jsp"%>
		<script type="text/javascript">
		Ext.onReady( function() {
		
			var root = new Ext.tree.TreeNode( {
				expanded : true
			});
		
			var authTree = new Ext.tree.TreePanel( {
				id : 'authTree',
				el : 'tree',
				margins : '5 0 5 5',
				region : 'west',
				width : 200,
				height : 560,
				minSize : 175,
				maxSize : 400,
				border : true,
				rootVisible : false,
				autoScroll : true,
				text : '',
				root : root
			});
		
			var nodes = [];
		
			OfficeService.getOfficeTreeAll({
				callback : function(result) {
					Ext.each(result, function(func) {
						var nodeFunction = new Ext.tree.TreeNode({
							id : 'sys-' + func.officeCode,
							iconCls : 'k-tree',
							text : func.officeName
						});
		
						nodeFunction.data = func;
						root.appendChild(nodeFunction);
						nodes.push(nodeFunction);
					});
				},
				async : false
			});
		
			for ( var i = 0; i < nodes.length; i++) {
				var node = nodes[i];
				var func = node.data;
				 
				if (func.superiorOfficeCode && func.superiorOfficeCode != func.officeCode) {
						var parentNode = authTree.getNodeById('sys-' + func.superiorOfficeCode);
						if (parentNode != null && parentNode != undefined) {
							root.removeChild(node);
							parentNode.appendChild(node);
						} else {
							root.removeChild(node);
						}
				}
			}
		
			authTree.render();
		
			authTree.on('click', function(node) {
				$('#officeCode').attr('disabled',true);
				$('#btnDelete').attr('disabled',false);
				$('#btnSave').attr('disabled',false);
				
					var officeId=node.data.officeId;
		
					var queryInfo = {
							listType : 'list',
							conjunctionType : 'and',
							queryObjectName : 'com.wef.system.entity.Office'
					};
		
					var fields = [];
					var field = {};
					field.condition = "eq";
					field.fieldName = "officeId";
					field.fieldValueString = officeId;
					fields[0] = field;
					queryInfo.fields = fields;
		
					CommonService.findByQuery(queryInfo, function(result) {
						FormUtils.convertSimpleObject2Form(result[0], 'editForm');
						$("input[type=text]").each(function(){
							 if($(this).val()=="null"){
								 $(this).val("");
								}
						});
					});
				});
		});
		
		$(window).ready(function(){
			$("#officeCodeSelsect").autoSelect({
				url : "${ctx}",
				className:"com.wef.system.entity.Office",
				displayField : "officeCode,officeName",
				displayFieldI18n: "system.Office.officeCode,system.Office.officeName,system.Office.address",  //显示字段国际化,不需要可不设置
				width : 450,
				displayNum : 8,
				inTable : false,
				hiddenId : "officeId",
				canNotInput : true,
				queryField : "officeCode,officeName"
				
			});
		});
		
		function saveAction(){
			var errString = '';
			var officeCode = $("#officeCode").val();
			var officeName = $("#officeName").val();
			var superiorOfficeCode = $("#officeCodeSelsect").val();
			if(!(officeCode) || officeCode == ''){
				errString += String.format(system.Office.officeCodeNotNull);
			}
			if(!(officeName) || officeName == ''){
				errString += String.format(system.Office.officeNameNotNull);
			}
			if(!(superiorOfficeCode) || superiorOfficeCode == ''){
				errString += String.format(system.Office.superiorOfficeCodeNotNull);
			}
		
			if(errString != ''){
				alert(errString);
				return false;
			}
		
			OfficeService.officeCodeCount(officeCode, saveCallback);
		}
		
		function saveCallback(result)
		{
			
			
			var objs = [];
			var obj = {};
			obj = FormUtils.convertSimpleForm2Object('editForm',obj);	
			if(obj.officeId!=null && obj.officeId.length>0){
					obj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_MODIFIED;
			}else{
				if(result > 0)
				{
					alert("部门不能重复！");
					return false;
				}
				obj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_ADDED;
			}
			
			objs.push(obj);
			OfficeService.saveOrUpdateOffices(objs,function(result){
					FormUtils.convertSimpleObject2Form(result[0], 'editForm');
					alert("保存成功.");
					window.location.reload();
				});	
		}
		
		function deleteAction(){
			var obj = {};
			obj = FormUtils.convertSimpleForm2Object('editForm',obj);
			
				obj.rowStatus = Gfa.GlobalConstant.ROWSTATUS_DELETED;
				if(window.confirm(i18n.msg.confirmDelete)){
					OfficeService.saveOrUpdateOffice(obj,function(){
						alert("删除成功.");
						window.location.reload();
					});	
				}
				$('#btnDelete').attr('disabled',true);
		}
		
		function newAction(){
			var obj = {};
			obj = FormUtils.convertSimpleForm2Object('editForm',obj);
			FormUtils.clearForm('editForm');
			if(obj.officeId!=''){
				$('#officeCodeSelsect').val(obj.officeCode);
			}
			$('#btnSave').attr('disabled',false);
			$('#btnDelete').attr('disabled',true);
			$('#officeCode').attr('disabled',false);
		};
		</script>
	</head>
	<body>
		<table width="100%" height="100%" cellpadding="0" cellspacing="0" >
			<tr>
				<td width="182" height="50%">
					<div id="tree" style="width:90%;height:90%"></div>	
				</td>
				
				<td valign="top">
					<div id="officeManagerEditDiv" class="ModuleEdit" >
						 <div class="ModuleEditBorder">
								<div class="ModuleEditButton">
									<!--按钮 -->
								    <g:button styleId="btnSave" disabled="true" styleClass="new" type="button" text="button.save" onclick="saveAction();"/>
									<!--按钮 -->
								    <g:button styleId="btnNew" styleClass="delete" type="button" text="button.new" onclick ="newAction();"/>
									<!--按钮 -->
								    <g:button styleId="btnDelete" disabled="true" styleClass="cancel" type="button" text="button.delete" onclick = "deleteAction();"/>
								</div>
								<div class="Line"></div>
								<div id="editFormDiv">
								    <form id="editForm" onsubmit="return false;">
								    <input type="hidden" name="officeId" id="hiddenOfficeId" >
								    <input type="hidden" name="rowStatus" id="rowStatus" >
								    <input type="hidden" name="companyId" id="companyId" >		
								    <input type="hidden" name="custId" id="custId" >
								    <input type="hidden" name="blContent" id="blContent" >	
								    <input type="hidden" name="functionType" id="functionType" >	
								    <input type="hidden" name="autoInternal" id="autoInternal" >	
								    <input type="hidden" name="inputUser" id="inputUser" > 
								    <input type="hidden" name="inputOffice" id="inputOffice" > 
								    <input type="hidden" name="useSystem" id="useSystem" >  
								    <input type="hidden" name="countryId" id="countryId" >  
								    <input type="hidden" name="cityId" id="cityId" > 
								    <input type="hidden" name="isDept" id="isDept" > 
								    <input type="hidden" name="officeRelactionTag" id="officeRelactionTag" >
								    <input type="hidden" name="inputUserName" id="inputUserName" >
								    <input type="hidden" name="isInternal" id="isInternal" > 
								    <input type="hidden" name="recordVersion" id="recordVersion" >
								    <input type="hidden" name="regionId" id="regionId" >
								    <input type="hidden" name="regionCode" id="regionCode" >
								    <input type="hidden" name="portId" id="portId" >
								    <input type="hidden" name="portCode" id="portCode" >
								    <input type="hidden" name="countryCode" id="countryCode" >
								    <input type="hidden" name="cityCode" id="cityCode" >
								    <input type="hidden" name="contact" id="contact" >
								    <input type="hidden" name="custCode" id="custCode" >
								    <input type="hidden" name="companyCode" id="companyCode" >
								    <input type="hidden" name="principalGroupCode" id="principalGroupCode" >
								    <input type="hidden" name="departmentId" id="departmentId" >
								              
									<table width="100%" class="ModuleEditInput">
											<tr>
												<td width="10%"><g:label text="<%=OfficeDomain.officeCode_full%>" allowBlank="false" /></td>
												<td width="22%"><g:text property="officeCode"></g:text></td>
												<td width="10%"><g:label text="<%=OfficeDomain.officeName_full%>" allowBlank="false"/></td>
												<td width="22%"><g:text property="officeName"/></td>
												<td width="10%"><g:label text="<%=OfficeDomain.address_full%>"/></td>
												<td width="22%"><g:text property="address"/></td>
											</tr>
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.homeCurrency_full%>" /></td>
												<td width="22%"><g:text property="homeCurrency" ></g:text></td>
												<td width="10%"><g:label text="<%=OfficeDomain.xchgrName_full%>"/></td>
												<td width="22%"><g:text property="xchgrName"/></td> --%>
												<td width="10%"><g:label text="<%=OfficeDomain.officeType_full%>" /></td>
												<td width="22%">
												<g:combox kind="select" property="officeType">
														<g:option value="1">AGENT</g:option>
														<g:option value="2">COMPANY</g:option>
														<g:option value="3">DEPARTMENT</g:option>
														<g:option value="4">OFFICE</g:option>
													</g:combox>
												</td>
												<td width="10%"><g:label text="<%=OfficeDomain.email_full%>"/></td>
												<td width="22%"><g:text property="email" isEmail="true" /></td>
												<td width="10%"><g:label text="<%=OfficeDomain.superiorOfficeCode_full%>"/></td>
												<td width="22%"><g:combox kind="associate" styleId="officeCodeSelsect" property="superiorOfficeCode" ></g:combox></td>
											</tr>	
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.language_full%>"/></td>
												<td width="22%"><g:text property="language" ></g:text></td>
												<td width="10%"><g:label text="<%=OfficeDomain.tel_full%>"/></td>
												<td width="22%"><g:text property="tel" isPhoneNumber="true"/></td>
												<td width="10%"><g:label text="<%=OfficeDomain.fax_full%>"/></td>
												<td width="22%"><g:text property="fax" isPhoneNumber="true"/></td> --%>
											</tr>
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.settleOffice_full%>"/></td>
												<td width="22%"><g:text property="settleOffice"></g:text></td>
												<td width="10%"><g:label text="<%=OfficeDomain.taxRegisterNo_full%>"/></td>
												<td width="22%"><g:text property="taxRegisterNo"/></td>
												<td width="10%"><g:label text="<%=OfficeDomain.businessRegisterNo_full%>"/></td>
												<td width="22%"><g:text property="businessRegisterNo"/></td> --%>
											</tr>
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.officeNativeName_full%>"/></td>
												<td width="22%"><g:text property="officeNativeName"></g:text></td> --%>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.abbrev_full%>"/></td>
												<td width="22%"><g:text property="abbrev"/></td> --%>
											<%-- 	<td width="10%"><g:label text="<%=OfficeDomain.email_full%>"/></td>
												<td width="22%"><g:text property="email" isEmail="true" /></td> --%>
											</tr>
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.superiorOfficeCode_full%>"/></td>
												<td width="22%"><g:combox kind="associate" styleId="officeCodeSelsect" property="superiorOfficeCode" ></g:combox></td> --%>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.isCustomer_full%>"/></td>
												<td width="22%"><g:checkbox property="isCustomer" value="true"/></td>
												<td width="10%"><g:label text="<%=OfficeDomain.active_full%>"/></td>
												<td width="22%"><g:checkbox property="active" value="true"/></td> --%>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.active_full%>"/></td>
												<td width="22%"><g:checkbox property="active" value="true"/></td>  --%>
											</tr>
											<tr>
												<%-- <td width="10%"><g:label text="<%=OfficeDomain.deleted_full%>"/></td>
												<td width="22%"><g:checkbox property="deleted" value="true"></g:checkbox></td> --%>
												<td width="10%">&nbsp;</td>
												<td width="22%">&nbsp;</td>
												<td width="10%">&nbsp;</td>
												<td width="22%">&nbsp;</td>
											</tr>
									</table>
									</form>
								</div>
						</div>
						<!-- 编辑区域 -->
					</div>
				</td>
			</tr>
		</table>
	</body>
</html>